package com.epfresh.api.widget.Dialog;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.epfresh.api.R;
import com.epfresh.api.utils.T;


public class BaseDialogView {

    public interface OnDismissListener {
        public void onDismiss(Object o);
    }

    protected Context context;
    protected ViewGroup contentContainer;
    protected ViewGroup decorView;//activity的根View
    protected ViewGroup rootView;//附加View 的 根View
    protected View vContentBg;//

    protected OnDismissListener onDismissListener;
    protected OnDismissListener onDismissImmediatelyListener;
    protected boolean isDismissing;

//    protected Animation outAnim1;
//    protected Animation inAnim1;
    protected Animation outAnim;
    protected Animation inAnim;
    protected boolean isInAnim = true;
    protected boolean isOutAnim = true;
    protected int gravity = Gravity.BOTTOM;

    protected int rootViewBg = Color.parseColor("#60000000");

    protected View.OnClickListener onErrorClickListener;
    
    public int outContainerRes;
    protected int containerRes;
    protected int rootRes;
    protected int rootBgRes;

    protected long inBgDur = 300;
    protected long outBgDur = 300;

    public BaseDialogView(Context context) {
        this.context = context;
        initRes();
        initBefore();
        initViews();
        initAnim();
        initEvents();
    }

    public long getInBgDur() {
        return inBgDur;
    }

    public void setInBgDur(long inBgDur) {
        this.inBgDur = inBgDur;
    }

    public long getOutBgDur() {
        return outBgDur;
    }

    public void setOutBgDur(long outBgDur) {
        this.outBgDur = outBgDur;
    }

    protected void initRes(){
        outContainerRes = R.id.out_container;
        containerRes = R.id.content_container;
        rootRes = R.layout.layout_base_dialog_view;
        rootBgRes = R.id.v_content_bg;
    }

    protected ViewGroup getDecorView(Context context) {
        return (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
    }


    public boolean isInAnim() {
        return isInAnim;
    }

    public void setInAnim(boolean isAnim) {
        this.isInAnim = isAnim;
        if (isInAnim) {
            inAnim.setDuration(300);
//            inAnim1.setDuration(200);
        }else {
            inAnim.setDuration(0);
//            inAnim1.setDuration(0);
        }

    }

    public boolean isOutAnim() {
        return isOutAnim;
    }

    public void setOutAnim(boolean isAnim) {
        isOutAnim = isAnim;

        if (isOutAnim) {
            outAnim.setDuration(300);
//            outAnim1.setDuration(300);
        }else {
            outAnim.setDuration(0);
//            outAnim1.setDuration(0);
        }
    }

    public void setAnim(boolean anim) {
        setInAnim(anim);
        setOutAnim(anim);
    }

    public void initBefore() {

    }
    
    

    protected void initViews() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        if (decorView == null)
            decorView = getDecorView(context);
        if (rootView == null) {
            rootView = (ViewGroup) layoutInflater.inflate(rootRes, null, false);
            rootView.setLayoutParams(new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
            ));
            rootView.setBackgroundColor(rootViewBg);
            contentContainer = (ViewGroup) rootView.findViewById(containerRes);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, gravity
            );
            contentContainer.setLayoutParams(params);
            vContentBg = rootView.findViewById(rootBgRes);
        }
    }

    protected void createdView(){

    }

    public View.OnClickListener getOnErrorClickListener() {
        return onErrorClickListener;
    }

    public void setOnErrorClickListener(View.OnClickListener onErrorClickListener) {
        this.onErrorClickListener = onErrorClickListener;
    }

    public OnDismissListener getOnDismissImmediatelyListener() {
        return onDismissImmediatelyListener;
    }

    public void setOnDismissImmediatelyListener(OnDismissListener onDismissImmediatelyListener) {
        this.onDismissImmediatelyListener = onDismissImmediatelyListener;
    }

    protected void initAnim() {
//        ObjectAnimator anim = ObjectAnimator.ofFloat(text, "alpha", 1f, 0.1f, 1f, 0.5f, 1f);
        inAnim = getInAnimation();
        outAnim = getOutAnimation();
       /* inAnim1 = new AlphaAnimation(0.0f, 1.0f);
        outAnim1 = new AlphaAnimation(1.0f, 0.0f);
        outAnim1.setFillAfter(true);
        outAnim1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (vContentBg != null) {
                    vContentBg.setBackgroundColor(0x00000000);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });*/
        setOutAnim(true);
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    protected void initEvents() {
    }

    public void setRootViewBackground(int color) {
        this.rootViewBg = color;
    }

    /**
     * show的时候调用
     *
     * @param view 这个View
     */
    private void onAttached(View view) {
        decorView.addView(view);
        startInAnimation();
        startInBgAnimation();
    }

    protected void startInAnimation() {
        contentContainer.startAnimation(inAnim);
    }

    protected void startOutAnimation() {
        contentContainer.startAnimation(outAnim);
    }


    boolean isShowing;

    /**
     * 添加这个View到Activity的根视图
     */
    public void show() {
        if (isShowing()) {
            return;
        }
        initViews();
        createdView();
        rootView.setVisibility(View.VISIBLE);
        isShowing = true;
        isDismissing = false;
        onAttached(rootView);
    }

    /**
     * 检测该View是不是已经添加到根视图
     *
     * @return 如果视图已经存在该View返回true
     */
    public boolean isShowing() {
       /* View view = decorView.findViewById(outContainerRes);
        return view != null;*/
        return isShowing;
    }

    protected Animation.AnimationListener animation = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            isDismissing = true;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            isDismissing = false;
            isShowing = false;
            rootView.setVisibility(View.INVISIBLE);
            if (onDismissListener != null) {
                onDismissListener.onDismiss(BaseDialogView.this);
            }

            //从activity根视图移除
            decorView.removeView(rootView);
            rootView = null;
           /* decorView.post(new Runnable() {
                @Override
                public void run() {

                    if (onDismissListener != null) {
                        onDismissListener.onDismiss(BaseDialogView.this);
                    }

                    //从activity根视图移除
                    decorView.removeView(rootView);
                    rootView = null;
                }
            });*/
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };

    public void dismiss() {

        if (isDismissing) {
            return;
        }
        if (!isShowing()) {
            return;
        }

        if (onDismissImmediatelyListener != null){
            onDismissImmediatelyListener.onDismiss(null);
        }
//        rootView.startAnimation(outAnim1);
        startOutBgAnimation();
        //消失动画
        outAnim.setAnimationListener(animation);
        startOutAnimation();
        isDismissing = true;
    }


    public void startInBgAnimation() {
        if (vContentBg != null) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(vContentBg, "alpha", 0f, 1f);
            animator.setDuration(inBgDur);//时间
            animator.start();
//            vContentBg.startAnimation(inAnim1);
        }
    }

    public void startOutBgAnimation() {
        if (vContentBg != null){
            ObjectAnimator animator = ObjectAnimator.ofFloat(vContentBg, "alpha", 1f, 0f);
            animator.setDuration(outBgDur);//时间
            animator.start();
        }
    }

    public Animation getInAnimation() {
        int res = getAnimationResource(this.gravity, true);
        return AnimationUtils.loadAnimation(context, res);
    }

    public Animation getOutAnimation() {
        int res = getAnimationResource(this.gravity, false);
        return AnimationUtils.loadAnimation(context, res);
    }

    public BaseDialogView setOnDismissListener(OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
        return this;
    }

    public BaseDialogView setCancelable(boolean isCancelable) {
        View view = rootView.findViewById(outContainerRes);

        if (isCancelable) {
            view.setOnClickListener(onCancelableTouchListener);
        } else {
            view.setOnTouchListener(null);
        }
        return this;
    }

    /**
     * Called when the user touch on black overlay in order to dismiss the dialog
     */
    private final View.OnClickListener onCancelableTouchListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }

    };



    public View findViewById(int id) {
        return contentContainer.findViewById(id);
    }

    private int INVALID = -1;

    public int getAnimationResource(int gravity, boolean isInAnimation) {
        switch (gravity) {
            case Gravity.BOTTOM:
                return isInAnimation ? R.anim.push_up_in : R.anim.push_up_out;
            case Gravity.RIGHT:
                return isInAnimation ? R.anim.anim_right_push_in : R.anim.anim_right_push_out;
            case Gravity.TOP:
                return isInAnimation ? R.anim.anim_top_push_in : R.anim.anim_top_push_out;
            case Gravity.CENTER:
                return isInAnimation ? R.anim.anim_fade_in : R.anim.anim_fade_out;
        }
        return INVALID;
    }
    
    public void dismissRootView(final Activity context) {
        if (context != null) {
            final ViewGroup decorView = (ViewGroup) context.getWindow().getDecorView().findViewById(android.R.id.content);
            decorView.post(new Runnable() {
                @Override
                public void run() {
                    //从activity根视图移除
                    View rootView = context.findViewById(outContainerRes);
                    if (rootView != null)
                    decorView.removeView(rootView);

                   
                }
            });
        }
    }


    public LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(context);
    }


    public void setContentView(View view) {
        contentContainer.removeAllViews();
        contentContainer.addView(view);
    }

    public void setContentView(int resId) {
        contentContainer.removeAllViews();
        getLayoutInflater().inflate(resId, contentContainer);
    }



    public void finish() {


        dismiss();
        onDestroy();
    }

    public void onDestroy() {

    }

    public void onBackPressed() {

        finish();
    }

    public <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

    public void toast(String content) {
        T.toast(content);
    }


    public String getString(int resId) {
        return context.getString(resId);
    }



}
